package com.pearadmin.modules.sys.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.pearadmin.common.tools.SequenceUtil;
import com.pearadmin.common.web.domain.request.PageDomain;
import com.pearadmin.modules.sys.domain.InvAnode;
import com.pearadmin.modules.sys.domain.InvAnodeInputInfo;
import com.pearadmin.modules.sys.domain.WorkCasting;
import com.pearadmin.modules.sys.mapper.InvAnodeInputInfoMapper;
import com.pearadmin.modules.sys.mapper.InvAnodeMapper;
import com.pearadmin.modules.sys.mapper.WorkCastingMapper;
import com.pearadmin.modules.sys.service.IWorkCastingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

/**
 * 浇筑工作记录Service业务层处理
 *
 * @author Mr.Ling
 * @date 2021-11-26
 */
@Service
public class WorkCastingServiceImpl implements IWorkCastingService {
    @Autowired
    private WorkCastingMapper workCastingMapper;
    @Autowired
    private InvAnodeMapper invAnodeMapper;
    @Autowired
    private InvAnodeInputInfoMapper invAnodeInputInfoMapper;


    /**
     * 查询浇筑工作记录
     *
     * @param id 浇筑工作记录ID
     * @return 浇筑工作记录
     */
    @Override
    public WorkCasting selectWorkCastingById(String id) {
        return workCastingMapper.selectWorkCastingById(id);
    }

    /**
     * 查询浇筑工作记录列表
     *
     * @param workCasting 浇筑工作记录
     * @return 浇筑工作记录
     */
    @Override
    public List<WorkCasting> selectWorkCastingList(WorkCasting workCasting) {
        return workCastingMapper.selectWorkCastingList(workCasting);
    }

    /**
     * 查询浇筑工作记录
     *
     * @param workCasting 浇筑工作记录
     * @param pageDomain
     * @return 浇筑工作记录 分页集合
     */
    @Override
    public PageInfo<WorkCasting> selectWorkCastingPage(WorkCasting workCasting, PageDomain pageDomain) {
        PageHelper.startPage(pageDomain.getPage(), pageDomain.getLimit());
        List<WorkCasting> data = workCastingMapper.selectWorkCastingList(workCasting);
        return new PageInfo<>(data);
    }

    /**
     * 新增浇筑工作记录
     *
     * @param casting 浇筑工作记录 和 阳极库存
     * @return 结果
     */
    @Override
    @Transactional
    public int insertWorkCasting(WorkCasting casting) {
        Map<String, Object> params = casting.getParams();
        List<Object> invAnodes = (List<Object>) params.get("invAnodes");
        casting.setNum((long)invAnodes.size());
        for (Object object : invAnodes) {
            //判断导杆投入信息
            InvAnode ia = BeanUtil.copyProperties(object, InvAnode.class);
            int num = casting.getOperateType().equals("out") ? -1 : 1;
            ia.setNum(num);
            ia.setId(SequenceUtil.makeStringId());
            ia.setCastingId(casting.getId());
            ia.setDeptId(casting.getDeptId());
            ia.setDeptName(casting.getDeptName());
            ia.setCreateTime(LocalDateTime.now());
            ia.setCreateBy(casting.getCreateBy());
            ia.setCreateName(casting.getCreateName());
            invAnodeMapper.insertInvAnode(ia);
            //新增导杆投入信息
            InvAnodeInputInfo inputInfo = new InvAnodeInputInfo();
            inputInfo.setAnodeCode(ia.getAnodeCode());
            inputInfo.setCreateTime(LocalDateTime.now());
            inputInfo.setType(ia.getBlockType());
            inputInfo.setDeptName(casting.getDeptName());
            inputInfo.setId(SequenceUtil.makeStringId());
           // todo 新增的时候需要修改导杆使用情况
            inputInfo.setStatus("0");
            invAnodeInputInfoMapper.insertInvAnodeInputInfo(inputInfo);
        }
        return workCastingMapper.insertWorkCasting(casting);
    }

    /**
     * 修改浇筑工作记录
     *
     * @param workCasting 浇筑工作记录
     * @return 结果
     */
    @Override
    public int updateWorkCasting(WorkCasting workCasting) {
        return workCastingMapper.updateWorkCasting(workCasting);
    }

    /**
     * 删除浇筑工作记录对象
     *
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteWorkCastingByIds(String[] ids) {
        return workCastingMapper.deleteWorkCastingByIds(ids);
    }

    /**
     * 删除浇筑工作记录信息
     *
     * @param id 浇筑工作记录ID
     * @return 结果
     */
    @Override
    public int deleteWorkCastingById(String id) {
        return workCastingMapper.deleteWorkCastingById(id);
    }
}
